'use strict';

var isFunction = require('./util.js').isFunction;
var base64 = require('./base64.js');
var md5 = require('./md5.js');
var config = require('../config/config');

function getAuth() {
  var clientId = config.clientId;
  var clientKey = config.clientKey;
  var timestamp = Date.now();
  var sign = md5(clientId + ':' + timestamp + ':' + clientKey);
  var auth = base64.btoa(clientId + ':' + timestamp + ':' + sign);
  return 'Basic ' + auth;
}

  var HOST_URI = 'https://sit.tyforce.com/traveltickets';
  // var HOST_URI = 'http://localhost';

function request(url, method, data, cb) {

  var folivoreSession = wx.getStorageSync('folivoreSession');
  var header = {
    'Authorization': 1,
    'appId': config.appId,
    'wxappversion': config.version
  };
  if (folivoreSession) {
    header.folivoreSession = folivoreSession;
  } else {
    header.folivoreSession = 1;
  }

  wx.request({
    url: url,
    data: data,
    method: method || 'GET',
    header: header,
    success: function success(res) {
      if (res && res.statusCode == 200) {
        if (res.data.result) {
          typeof cb == "function" && cb(res.data.data);
        } else {
          var err = { msg: res.data.errorMessage };
          typeof cb == "function" && cb(undefined, err);
        }
      } else {
        var err = { msg: res.errMsg };
        typeof cb == "function" && cb(undefined, err);
      }
    },
    fail: function fail(error) {
      console.log("wx.request error:", error);
      var err = { msg: error.errorMessage };
      typeof cb == "function" && cb(undefined, err);
    }
  });
}

module.exports = {

  // 请求方法
  request: request,
  //车辆列表请求接口
  ticketRemainder: function ticketRemainder() {
    return HOST_URI + '/api/ticket/query/ticketRemainder';
  },
  //扫码检票
  scanQrCode: function scanQrCode() {
    return HOST_URI + '/api/qrcode/scanQrCode';
  },
  checkToken: function checkToken() {
    return HOST_URI + '/api/passport/auth/checksession';
  },
  getConect: function getConect() {
    return HOST_URI + '/api/contacts/getFrequentContactListByOpendId';
  },
  getMyOrder: function getMyOrder() {
    return HOST_URI + '/api/trainOrder/search';
  },
  getOrderDetailUrl: function getOrderDetailUrl() {
     return HOST_URI + '/api/trainOrder/searchOne';
  },
  //根据opendId获取联系人列表
  getConect: function getConect() {
    return HOST_URI + '/api/contacts/getFrequentContactListByOpendId';
  },
  //根据联系人ID查询联系人
  getContactById: function getContactById() {
    return HOST_URI + '/api/contacts/getFrequentContactById';
  },
  //根据联系人ID更新联系人
  updateFrequentContact: function updateFrequentContact() {
    return HOST_URI + '/api/contacts/updateFrequentContact';
  },
  //添加联系人
  addFrequentContact: function addFrequentContact(){
    return HOST_URI + '/api/contacts/addFrequentContact';
  },
  getTicketDetailUrl: function getTicketDetailUrl() {
    return HOST_URI + '/api/ticket/query/ticketRemainderOne';
  },
  //添加更新小程序用户信息
  addOrUpdateUser: function addOrUpdateUser(){
    return HOST_URI + '/api/wxMembers/addOrUpdate';
  },
  //创建订单url
  createOrderUrl: function createOrderUrl() {
    return HOST_URI + '/api/trainOrder/create';
  },
  //查询时刻表
  getTimeTable: function getTimeTable(){
    return HOST_URI + '/api/ticket/query/stationTime';
  },
  //获取opendId
  getOpendId: function getOpendId(){
    return HOST_URI + '/api/common/getOpendId';
  },
  //退票
  refundTicket: function refundTicket(){
    return HOST_URI + '/api/trainOrder/refundTicketApply';
  },
  //删除联系人
  delContact: function delContact(){
    return HOST_URI + '/api/contacts/delFrequentContactById';
  },
  //取消订单
  cancelOrder: function cancelOrder(){
    return HOST_URI + '/api/trainOrder/invailOrder';
  },//订单唤起支付
	payOrder: function payOrder() {
	  return HOST_URI + '/api/trainOrder/payOrder';
  },//支付回调
  payCallBack: function payCallBack() {
    return HOST_URI + '/payCallback/wxPayNotify';
  },
  searchRefundTicket: function searchRefundTicket() {
    return HOST_URI + '/api/trainOrder/searchRefundTicket';
  },
  getCheckStations: function getCheckStations() {
    return HOST_URI + '/api/ticketCheckStation/getCheckStation';
  }, checkTicket: function checkTicket(){
    return HOST_URI + '/api/ticketCheckStation/checkTicket';
  }, getQrCode: function getQrCode(){
    return HOST_URI + '/api/qrcode/createQrCode';
  }
};
